package edu.kit.aifb.ruleintegrator.rule;

import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.semanticweb.yars.nx.Resource;

import edu.kit.aifb.ats.triples.model.Pattern;
import edu.kit.aifb.ruleintegrator.rule.metric.Function;

public class MetricPredicate extends Predicate{

	private Resource m_att1;
	private Resource m_att2;
	private Function m_metric;
	private float m_threshold;
	 

	public MetricPredicate(Resource att1,Resource att2,Function metric,float threshold){
		this.m_att1=att1;
		this.m_att2=att2;
		this.m_metric=metric;
		this.m_threshold=threshold;
	}

	@Override
	public boolean equals(Object obj) {
		 if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (obj.getClass() != getClass()) {
		     return false;
		   }
		   MetricPredicate rhs = (MetricPredicate) obj;
		   return new EqualsBuilder()
		                 .append(this.m_att1.toN3(), rhs.m_att1.toN3())
		                 .append(this.m_att2.toN3(), rhs.m_att2.toN3())
		                 .append(this.m_metric, rhs.m_metric)
		                 .isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append( (m_att1 == null ? 0 : m_att1.hashCode()))
				.append((m_att2 == null ? 0 : m_att2.hashCode()))
				.append((m_metric == null ? 0 : m_metric.hashCode()))
				.toHashCode();
//		hash = hash * 31 + (Float.floatToIntBits(m_threshold));
	}

	@Override
	public List<Pattern> getPatterns(Signature sign) {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean isSimilar(String value1,String value2){

		if(value1==null || value1.isEmpty()){
			return false;
		}
		if(value2==null || value2.isEmpty()){
			return false;
		}


		if(this.m_metric.measure(value1, value2) >= m_threshold){
			return true;
		}
		return false;
	}



}
